import { ShapeType, ObjectType, CombinedState } from 'reducers';
import { rememberObject } from 'actions/annotation-actions';

import { useDispatch, useSelector } from 'react-redux';
import { message } from 'antd';

export function useControlsDraw() {
    const dispatch = useDispatch();
    const canvasInstance = useSelector((state: CombinedState) => state.annotation.canvas.instance)!;
    const labels = useSelector((state: CombinedState) => state.annotation.job.labels);

    function onDraw(shapeType: ShapeType): void {
        const labelId = labels.length ? labels[0].id : 0;
        if (!labelId) {
            message.error('Please configure a label first');
            return;
        }
        canvasInstance.cancel();
        canvasInstance.draw({
            enabled: true,
            rectDrawingMethod: undefined,
            cuboidDrawingMethod: undefined,
            numberOfPoints: undefined,
            shapeType,
            skeletonSVG: undefined,
            crosshair: [ShapeType.RECTANGLE, ShapeType.CUBOID, ShapeType.ELLIPSE].includes(ShapeType.POLYGON),
        });
        dispatch(
            rememberObject({
                activeObjectType: ObjectType.SHAPE,
                activeShapeType: shapeType,
                activeLabelID: labelId,
                activeNumOfPoints: undefined,
                activeRectDrawingMethod: undefined,
                activeCuboidDrawingMethod: undefined,
            }),
        );
    }

    return {
        onDraw,
    };
}
